 <template>
  <div>
    <p></p>
    <!--  -->
    <el-card class="box-card" style="margin-top: 20px">
      <div style="width: 600px; display: flex">
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          class="input-with-select"
          v-model="commodityForm.query"
          clearable
          @clear="getCommodity"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getCommodity"
          ></el-button> </el-input
        ><!-- 添加商品 -->
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <el-button type="primary" @click="addpage">添加商品</el-button>
        <!-- </span>  -->
      </div>
      <el-table :data="commodityList" border style="width: 100%">
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="700"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="280">
          <template v-slot="scope">
            {{ scope.row.add_time }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230">
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delCommodity(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="commodityForm.pagenum"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="commodityForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求商品列表的配置 语义化都是按照视频统一标准来的
      commodityForm: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      totals: 0, //总页术
      commodityList: [],
    };
  },

  mounted() {
    this.getCommodity();
  },

  methods: {
    //   获取数据
    async getCommodity() {
      this.$axios
        .get("https://www.liulongbin.top:8888/api/private/v1/goods", {
          params: this.commodityForm,
        })
        .then((res) => {
          let { data, meta } = res;
          console.log(res);
          if (meta.status == 200) {
            this.commodityList = data.goods;
            this.totals = data.total;
            // 修改日期的函数
            function add0(m) {
              return m < 10 ? "0" + m : m;
            }
            function formatDate(timeStamp) {
              let time = new Date(timeStamp),
                y = time.getFullYear(),
                m = time.getMonth() + 1,
                d = time.getDate(),
                h = time.getHours(),
                mm = time.getMinutes(),
                s = time.getSeconds();

              return (
                y +
                "-" +
                add0(m) +
                "-" +
                add0(d) +
                " " +
                add0(h) +
                ":" +
                add0(mm) +
                ":" +
                add0(s)
              );
            }
            this.commodityList.map((a) => {
              a.add_time = formatDate(a.add_time);
              return console.log(a.add_time);
            });
          }
        });
    },
    handleSizeChange(val) {
      this.commodityForm.pagesize = val;
      this.getCommodity();
    },
    handleCurrentChange(val) {
      this.commodityForm.pagenum = val;
      this.getCommodity();
    },
    delCommodity(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$axios.delete(
            `goods/${id}`
          );
          let { data, meta } = res.data;
          if (meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            this.getCommodity();
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addpage() {
      this.$router.push("/addts");
    },
  },

  watch: {},
};
</script>

<style scoped></style>
















// 
